package com.dtjd.playcoinmonkey.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.dtjd.playcoinmonkey.ChildClick;
import com.dtjd.playcoinmonkey.bean.ManageTipOffBean;
import com.dtjd.playcoinmonkey.bean.ManageTipOffPostBean;
import com.dtjd.playcoinmonkey.bean.ManageTipOffReasonBean;

import java.util.List;

import com.dtjd.playcoinmonkey.R;
public class TipOffExpandAdapter extends BaseExpandableListAdapter {
    private final List<ManageTipOffBean> manageTipOffBeanList;
    private final Context context;
    private final ChildClick frozen,viewDetail;
    public TipOffExpandAdapter(Context context, List<ManageTipOffBean> commentBeanList, ChildClick frozen,ChildClick viewDetail) {
        this.context = context;
        this.manageTipOffBeanList = commentBeanList;
        this.frozen = frozen;
        this.viewDetail = viewDetail;
    }
    @Override
    public int getGroupCount() {
        return manageTipOffBeanList.size();
    }
    @Override
    public int getChildrenCount(int i) {
      return manageTipOffBeanList.get(i).manageTipOffReasonBeanList.size();
    }
    @Override
    public Object getGroup(int i) {
        return manageTipOffBeanList.get(i);
    }
    @Override
    public Object getChild(int i, int i1) {
        return manageTipOffBeanList.get(i).manageTipOffReasonBeanList.get(i1);
    }
    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return getCombinedChildId(groupPosition, childPosition);
    }
    @Override
    public boolean hasStableIds() {
        return true;
    }
    @SuppressLint("StringFormatMatches")
    @Override
    public View getGroupView(final int groupPosition, boolean isExpand, View convertView, ViewGroup viewGroup) {
        ManageTipOffPostBean bean = manageTipOffBeanList.get(groupPosition).manageTipOffPostBean;
        final GroupHolder groupHolder;
        if(convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.item_manage_tipoff_post, viewGroup, false);
            groupHolder = new GroupHolder(convertView);
            convertView.setTag(groupHolder);
        }else {
            groupHolder = (GroupHolder) convertView.getTag();
        }
        Glide.with(context).load(bean.wb_url).apply(RequestOptions.bitmapTransform(new CircleCrop())).placeholder(R.drawable.loading).placeholder(R.drawable.loading).error(R.drawable.loaderr).into(groupHolder.icon);
        groupHolder.content.setText(bean.content);
        groupHolder.title.setText(bean.title);
        groupHolder.name.setText(bean.wb_nickname);
        groupHolder.frozen.setOnClickListener(view -> frozen.click(view,groupPosition));
        if (bean.isTipoff){
            groupHolder.frozen.setText(context.getResources().getString(R.string.frozen));
            groupHolder.viewDetail.setVisibility(View.VISIBLE);
        }else {
            groupHolder.frozen.setText(context.getResources().getString(R.string.thaw));
            groupHolder.viewDetail.setVisibility(View.GONE);
        }
        if (bean.isColse){
            groupHolder.showmore.setText(context.getResources().getString(R.string.show_more));
        }else {
            groupHolder.showmore.setText(context.getResources().getString(R.string.collapse));
        }
        groupHolder.viewDetail.setOnClickListener(view -> viewDetail.click(view,groupPosition));
        return convertView;
    }
    @Override
    public View getChildView(final int groupPosition, int childPosition, boolean b, View convertView, ViewGroup viewGroup) {
        ManageTipOffReasonBean bean = manageTipOffBeanList.get(groupPosition).manageTipOffReasonBeanList.get(childPosition);
        final ChildHolder childHolder;
        if(convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.item_manage_tipoff_reason,viewGroup, false);
            childHolder = new ChildHolder(convertView);
            convertView.setTag(childHolder);
        } else {
            childHolder = (ChildHolder) convertView.getTag();
        }
        childHolder.reason.setText(bean.reason);
        childHolder.time.setText(bean.time);
        childHolder.label.setText(bean.label);
        return convertView;
    }
    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }
    private static class GroupHolder{
        private final ImageView icon;
        private final TextView name, content, title,frozen,showmore,viewDetail;
        public GroupHolder(View view) {
            icon = view.findViewById(R.id.item_manage_tipoff_post_icon);
            content = view.findViewById(R.id.item_manage_tipoff_post_content);
            name = view.findViewById(R.id.item_manage_tipoff_post_name);
            title = view.findViewById(R.id.item_manage_tipoff_post_title);
            frozen = view.findViewById(R.id.item_manage_tipoff_post_frozen);
            showmore = view.findViewById(R.id.item_manage_tipoff_post_show_more);
            viewDetail = view.findViewById(R.id.item_manage_tipoff_post_viewdetail);
        }
    }
    private static class ChildHolder{
        private final TextView reason, label,time;
        public ChildHolder(View view) {
            reason = view.findViewById(R.id.item_manage_tipoff_reason_reason);
            label = view.findViewById(R.id.item_manage_tipoff_reason_label);
            time = view.findViewById(R.id.item_manage_tipoff_reason_time);
        }
    }
}